package br.usp.ime.mac.mico.dao.tests;

import br.usp.ime.mac.mico.dao.JPASampleDAO;
import br.usp.ime.mac.mico.models.Sample;
import junit.framework.TestCase;

public class JPASampleDAOTest extends TestCase {

	private JPASampleDAO sampleDAO = new JPASampleDAO();
	private Sample sampleA         = new Sample();
	private Sample sampleB         = new Sample();
	private Sample sampleC         = new Sample();
	private String sampleACode     = "XVT-0972-A";
	private String sampleBCode     = "XVT-0972-B";
	private String sampleCCode     = "XVT-0972-C";

	@Override
	public void setUp() {
		sampleA.setLocalCode(sampleACode);
		sampleB.setLocalCode(sampleBCode);
		sampleC.setLocalCode(sampleCCode);
	}

	/*This test will fail if the database has just been cleaned.
	  TODO: Make a test that don't need an initialized database,
	  but will still test if the persistency works across program
	  runs.*/
	public void testPersistency(){
		Sample sampleD = sampleDAO.buscaPorId(1);
		assertNotNull(sampleD);
	}	

	public void testSalvar() {
		long sampleAID = sampleDAO.salvar(sampleA);
		long sampleBID = sampleDAO.salvar(sampleB);
		assertEquals(sampleDAO.buscaPorId(sampleAID)
				.getLocalCode(), sampleACode);
		sampleDAO.closePersistence();
		sampleDAO.openPersistence();
		assertEquals(sampleDAO.buscaPorId(sampleBID)
				.getLocalCode(), sampleBCode);
	}

	public void testAtualizar() {
		long sampleAID = sampleDAO.salvar(sampleA);
		sampleA.setLocalCode(sampleBCode);
		sampleDAO.atualizar(sampleB);
		sampleDAO.closePersistence();
		sampleDAO.openPersistence();
		assertEquals(sampleBCode,
				sampleDAO.buscaPorId(sampleAID).getLocalCode());
	}
}
